package com.web.base.auth.provider;

import com.blue.validation.ValidationUtil;
import com.blue.validation.group.SaveModel;
import com.blue.validation.group.UpdateModel;
import com.web.base.auth.cache.RoleMenuButtonCache;
import com.web.base.auth.cache.RoleUserCache;
import com.web.base.auth.exception.MenuButtonException;
import com.web.base.auth.facade.MenuButtonFacade;
import com.web.base.auth.message.MenuButtonRequest;
import com.web.base.auth.message.MenuButtonResponse;
import com.web.base.auth.model.MenuButton;
import com.web.base.auth.service.MenuButtonService;
import com.web.base.auth.util.MenuButtonConvertor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component("authMenuButtonProvider")
public class MenuButtonProvider implements MenuButtonFacade
{
	private static Logger logger = LoggerFactory.getLogger(MenuButtonProvider.class);

	@Autowired
	private MenuButtonService menuButtonService;
	@Autowired
	private RoleUserCache roleUserCache;
	@Autowired
	private RoleMenuButtonCache roleMenuButtonCache;

	public MenuButtonProvider()
	{
	}

	@Override
	public MenuButtonResponse add(MenuButtonRequest request)
	{
		ValidationUtil.valid(request, SaveModel.class);
		MenuButton menuButton = MenuButtonConvertor.toMenuButton(request);
		menuButton = menuButtonService.save(menuButton);
		MenuButtonResponse response = MenuButtonConvertor.toResponse(menuButton);
		return response;
	}

	@Override
	public MenuButtonResponse update(MenuButtonRequest request)
	{
		ValidationUtil.valid(request, UpdateModel.class);
		MenuButton menuButton = MenuButtonConvertor.toMenuButton(request);
		menuButton = menuButtonService.save(menuButton);
		MenuButtonResponse response = MenuButtonConvertor.toResponse(menuButton);
		return response;
	}

	@Override
	public MenuButtonResponse get(Integer id)
	{
		if (id == null)
			throw new MenuButtonException("菜单权限ID不能为空");
			
		MenuButton menuButton = menuButtonService.get(id);
		MenuButtonResponse response = MenuButtonConvertor.toResponse(menuButton);
		return response;
	}
	
	@Override
	public void delete(Integer id)
	{
		if (id == null)
			throw new MenuButtonException("菜单权限ID不能为空");
		
		MenuButton menuButton = new MenuButton();
		menuButton.setId(id);
		menuButtonService.delete(menuButton);
	}

	@Override
	public List<MenuButtonResponse> list(MenuButtonRequest request)
	{
		MenuButton menuButton = MenuButtonConvertor.toMenuButton(request);
		List<MenuButton> list = menuButtonService.list(menuButton);
		List<MenuButtonResponse> responseList = MenuButtonConvertor.toResponse(list);
		return responseList;
	}
	
}
